/*
 * XMLStandardErrorHandler.java
 * Created on 21.08.2005
 *
 * VirtualHockey.net Internet Platform
 * Copyright (c) 2005, Jan Janke
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hockeo.vhbase.utils.xml;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

/**
 * Standard error handler that shall be used when parsing XML documents.
 * 
 * @version $Id: XMLStandardErrorHandler.java 71 2005-08-27 16:31:58Z jankejan $
 * @author jankejan
 */
public class GXmlStdErrorHandler implements ErrorHandler
{
  private static Log         s_log          = LogFactory.getLog( GXmlStdErrorHandler.class );

  /** Counts the number of warnings that have been reported. */
  private int                d_nCntWarnings = 0;

  /** Counts the number of errors that have been reported. */
  private int                d_nCntErrors   = 0;

  /*
   * @see org.xml.sax.ErrorHandler#warning(org.xml.sax.SAXParseException)
   */
  public void warning( SAXParseException ex ) throws SAXException
  {
    d_nCntWarnings++;
    s_log.warn( "XML Parser Warning: " + ex.getMessage() );
  }

  /*
   * @see org.xml.sax.ErrorHandler#error(org.xml.sax.SAXParseException)
   */
  public void error( SAXParseException ex ) throws SAXException
  {
    d_nCntErrors++;
    s_log.error( "XML Parser Error: " + ex.getMessage() );
  }

  /*
   * @see org.xml.sax.ErrorHandler#fatalError(org.xml.sax.SAXParseException)
   */
  public void fatalError( SAXParseException ex ) throws SAXException
  {
    d_nCntErrors++;
    s_log.error( "XML Parser Fatal Error: " + ex.getMessage() );
  }

  /**
   * Returns whether any warnings have been reported to the error handler.
   * 
   * @return <code>true</code> if at least one warning was reported, <code>false</code>
   *         otherwise
   */
  public boolean hasWarnings()
  {
    return d_nCntWarnings > 0;
  }

  /**
   * Returns whether any errors (normal and fatal ones) have been reported to the error
   * handler.
   * 
   * @return <code>true</code> if at least one error was reported, <code>false</code>
   *         otherwise
   */
  public boolean hasErrors()
  {
    return d_nCntErrors > 0;
  }
}
